unit PaymentsEditFrm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, cxGraphics, cxControls, cxLookAndFeels,
  cxLookAndFeelPainters, dxSkinsCore, dxSkinOffice2013White, dxSkinscxPCPainter,
  cxStyles, cxCustomData, cxFilter, cxData, cxDataStorage, cxEdit, cxNavigator,
  Data.DB, cxDBData, cxContainer, dxLayoutcxEditAdapters, cxDBExtLookupComboBox,
  cxSpinEdit, dxLayoutContainer, cxTextEdit, cxMaskEdit, cxDropDownEdit,
  cxLookupEdit, cxDBLookupEdit, cxDBLookupComboBox, cxGridCustomTableView,
  cxGridTableView, cxGridBandedTableView, cxGridDBBandedTableView, cxClasses,
  cxGridCustomView, cxGrid, FIBDataSet, pFIBDataSet, dxLayoutControl,
  dxLayoutControlAdapters, Vcl.Menus, dxLayoutLookAndFeels, Vcl.StdCtrls,
  BaseForm,
  cxButtons;

type
  TPaymentsEditForm = class(TDezForm)
    grp1: TdxLayoutGroup;
    lc1: TdxLayoutControl;
    ds_SERVICES: TpFIBDataSet;
    src_SERVICES: TDataSource;
    ds_DOG: TpFIBDataSet;
    src_DOG: TDataSource;
    grdRep1: TcxGridViewRepository;
    v_DOG: TcxGridDBBandedTableView;
    cl_DOGCODE: TcxGridDBBandedColumn;
    cl_DOGKONTRAGENT: TcxGridDBBandedColumn;
    cl_DOGKNAME: TcxGridDBBandedColumn;
    cl_DOGNOMDOG: TcxGridDBBandedColumn;
    cl_DOGDATADOG: TcxGridDBBandedColumn;
    cl_DOGDATAENDDOG: TcxGridDBBandedColumn;
    cl_DOGDATAPATH: TcxGridDBBandedColumn;
    cl_DOGFILENAME: TcxGridDBBandedColumn;
    cl_DOGDOGDATA: TcxGridDBBandedColumn;
    cbb_SERVICES: TcxLookupComboBox;
    itmlc1Item1: TdxLayoutItem;
    spin_Tarif: TcxSpinEdit;
    itmlc1Item11: TdxLayoutItem;
    spinQuantity: TcxSpinEdit;
    itmlc1Item12: TdxLayoutItem;
    cbbKontr: TcxLookupComboBox;
    itmlc1Item13: TdxLayoutItem;
    cbbDOG: TcxExtLookupComboBox;
    itmlc1Item14: TdxLayoutItem;
    grplc1Group1: TdxLayoutGroup;
    btnSave: TcxButton;
    itmlc1Item15: TdxLayoutItem;
    btnCancel: TcxButton;
    itmlc1Item16: TdxLayoutItem;
    dxlytlkndflst1: TdxLayoutLookAndFeelList;
    dxlytcxlkndfl1: TdxLayoutCxLookAndFeel;
    procedure btnSaveClick(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure cbbKontrPropertiesEditValueChanged(Sender: TObject);
  private
    { Private declarations }
    ds: TpFIBDataSet;
    isNewRec: Boolean;
    procedure PrepareDS;
    procedure AddRec(isNew: Boolean);
    procedure FillControlls;
    procedure ReopenDog;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent; dsOut: TpFIBDataSet; isNew: Boolean = false);
  end;

var
  PaymentsEditForm: TPaymentsEditForm;

implementation

{$R *.dfm}

uses AppUtils, cmDBUnit;

{ TPaymentsEditForm }

procedure TPaymentsEditForm.AddRec(isNew: Boolean);
begin
  with ds do begin
   if isNew then
   Append else
   Edit;
      FN('SERVICES').AsVariant:= cbb_SERVICES.EditValue;
      FN('KONTRAGENT').AsVariant:= cbbKontr.EditValue;
      FN('DOG').AsVariant:= cbbDOG.EditValue;
      FN('TARIF').AsVariant:= spin_Tarif.EditValue;
      FN('QUANTITY').AsVariant:= spinQuantity.EditValue;
    Post;
  end;
end;

procedure TPaymentsEditForm.btnCancelClick(Sender: TObject);
begin
  ModalResult:= mrCancel;
end;

procedure TPaymentsEditForm.btnSaveClick(Sender: TObject);
begin
  AddRec(isNewRec);
  ModalResult:= mrOk;
end;

procedure TPaymentsEditForm.cbbKontrPropertiesEditValueChanged(Sender: TObject);
begin
  ReopenDog;
end;

constructor TPaymentsEditForm.Create(AOwner: TComponent; dsOut: TpFIBDataSet; isNew: Boolean = false);
begin
  inherited Create(AOwner);
  PrepareDS;
  ds:= dsOut;
  isNewRec:= isNew;
  if not isNew then FillControlls;
end;

procedure TPaymentsEditForm.FillControlls;
begin
  with ds do begin
    cbb_SERVICES.EditValue:=FN('SERVICES').AsVariant;
    cbbKontr.EditValue:=FN('KONTRAGENT').AsVariant;
    cbbDOG.EditValue:=FN('DOG').AsVariant;
    spin_Tarif.EditValue:=FN('TARIF').AsVariant;
    spinQuantity.EditValue:=FN('QUANTITY').AsVariant;
  end;
end;

procedure TPaymentsEditForm.PrepareDS;
begin
  ds_SERVICES.Open;
  cmDb.ds_KONTRAGENT.Open;
  //ds_DOG.Open;
end;

procedure TPaymentsEditForm.ReopenDog;
begin
  with ds_DOG do begin
    Close;
    MainWhereClause:='D.KONTRAGENT = :kontr';
    ParamByName('kontr').AsVariant:= cbbKontr.EditValue;
    Open;
  end;
end;

end.
